$(function () {
    /*验证用户名是否重复*/
    $.extend($.fn.validatebox.defaults.rules, {
            checkName: {
                validator: function(value, param){
                    /* 改为同步请求*/
                    var html = $.ajax({
                        url: "product/checkName",
                        data:{
                            productName:value,
                        },
                        async: false
                    }).responseText;
                    return html==="true";
                },
                message: '用户名已重复!'
            }
        }
    );

    /*抽取常量*/
    var AddDialog= $("#AddDialog");
    /*添加页面表格的对象*/
    var productAddDialogForm = $("#productAddDialogForm");
    /*显示所有商品信息的对象*/
    var productDatagrid=$("#productDatagrid");

    var proObj={
        /*搜索--高级查询*/
        search:function () {
            /* 先获取keyword输入的值*/
            var keyword=$("#keyword").val();
            /*alert(keyword);*/
            /*传入后台查询*/
            productDatagrid.datagrid("load",{"keyword":keyword});
        },

        /*新增商品*/
        add:function () {
            /*每次添加完成后清数数据*/
            productAddDialogForm.form("clear");
            AddDialog.dialog("open");
        },
        /*添加dialog的保存按钮*/
        save:function () {
           /* var url="product/saveOrupdate";*/
            productAddDialogForm.form('submit',{
                url:"product/saveOrupdate",
                success:function(data){
                    /* 处理返回参数转化为JSON格式*/
                    console.debug(data);
                    var result = $.parseJSON(data);
                    if(result.success){
                        //列表刷新
                        $.messager.alert("提示","恭喜您,保存成功,列表已刷新","info");
                        proObj.close();
                        /*刷新页面*/
                        productDatagrid.datagrid("reload");
                    }else{
                        $.messager.alert("提示","提交失败，请重试！");
                    }
                }
            });
        },

        /*添加dialog的关闭按钮*/
        close:function () {
            AddDialog.dialog("close");
        },
        /*删除的方法*/
        remove:function () {
            var row=productDatagrid.datagrid("getSelected")
            /*判断是否选中*/
            if(row){
                $.messager.confirm('提示', '确认删除？', function(r){
                    var id=row.id;
                    /* r为回调函数 */
                    if (r){
                        //发送ajax请求到后台进行删除
                        $.post("product/remove",{"id":id},function (data) {
                            if(data.success){
                                $.messager.alert("提示","恭喜您,删除成功,列表已经刷新","info");
                                productDatagrid.datagrid("reload");
                            }else{
                                $.messager.alert("提示","提交失败！！","error");
                            }
                        });
                    }
                });
            }
            else {
                $.messager.alert("提示","请选中需要删除的行！！","error");
            }
        },
        /* 修改的方法*/
        update: function () {
            var row = productDatagrid.datagrid("getSelected");
            if (row) {
                var id = row.id;
                if(row.productType){
                    row["type_id"]=row.productType.type;
                }
                productAddDialogForm.form("clear");
                /*先清空数据*/
                /*回显数据*/
                productAddDialogForm.form('load',row);
                $("#productName").textbox("disableValidation");
                $("#AddDialog").dialog("open").dialog("center").dialog("setTitle","修改商品信息")
            } else {
                $.messager.alert("提示", "请选中需要修改的数据哦", "error");
            }
        },


        /*导出的方法*/
        download:function () {
            var keyword=$('#keyword').val();
            window.location.href = "product/download?keyword="+keyword;
        }
    };

//统一绑定事件  标签下有data此属性被点击触发此方法
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        proObj[method]();
    });

    productDatagrid.datagrid({

        columns:[[
            {field:'id',title:'序号', width:'7%', align:'center',checkbox:true,},
            {field:'productName',title:'商品名', width:'7.5%', align:'center',},
            {field:'productCode',title:'助词码', width:'7.5%', align:'center',},
            {field:'encode',title:'商品编码', width:'7.5%', align:'center',},
            {field:'standard',title:'商品规格', width:'7.5%', align:'center',},
            {field:'standard',title:'商品单位', width:'7.5%', align:'center',},
            {field:'salePrice',title:'零售价', width:'7.5%', align:'center',
                styler: function(value,row,index) {
                    return 'background-color:#ffee00;color:red;';
                },formatter:function (v,r) {
                    return r.salePrice.toFixed(2);
                }},

            {field:'costPrice',title:'成本价', width:'7.5%', align:'center',
                formatter: function(v,r) {
                    return r.costPrice.toFixed(2);
                },styler:function (v,r) {
                    return 'background-color:red;color:black;';
                }},
            {field:'repertoryNum',title:'库存数量', width:'7.5%', align:'center',},
            {field:'joinDate',title:'入库时间', width:'7.5%', align:'center',},
            {field:'shop',title:'所属店铺', width:'7.5%', align:'center',},
            {field:'yieldly',title:'产地', width:'7.5%', align:'center',},
            {field:'expirationDate',title:'保质期', width:'7.5%', align:'center',},
            {field:'productType',title:'产品类型', width:'7.5%', align:'center',formatter:function (v,r) {
                    if(v){
                        return v.type;
                    }
                }},
        ]]
    });

    0
    /*新增的窗口默认关闭*/
    AddDialog.dialog({
        closed: true,
        modal: true,
        buttons:"#AddDialogButtons"
    });


})

